"""this script parse the content of a xml file"""
import binascii
import struct


class Convert():
    def str_to_bytes(self, str_data):
        """
        (中文/ascii) -> 字节
        "你好AA55" -> b'\xc4\xe3\xba\xc3AA55'
        """
        try:
            bytes_data = str_data.encode('GB2312')
        except Exception as e:
            print("str_to_bytes err(%s)" % e)
            bytes_data = b''
        return bytes_data

    def bytes_to_str(self, bytes_data):
        """
        字节 -> (中文/ascii)
        b'\xc4\xe3\xba\xc3AA55' -> "你好AA55"
        """
        try:
            str_data = bytes_data.decode('GB2312')
        except Exception as e:
            print("bytes_to_str err(%s)" % e)
            str_data = ""
        return str_data

    def hexstr_to_str(self, hexstr_data):
        """
        16进制字符串 -> (中文/ascii)
        "c4e3bac33536" -> "你好56"
        """
        try:
            str_data = binascii.a2b_hex(hexstr_data).decode('GB2312')
        except Exception as e:
            print("hexstr_to_str err(%s)" % e)
            str_data = ""
        return str_data

    def str_to_hexstr(self, str_data):
        """
        (中文/ascii) -> 16进制字符串
        "你好56" -> "c4e3bac33536"
        """
        try:
            bytes_data = self.str_to_bytes(str_data)
            hexstr_data = self.bytes_to_str(binascii.b2a_hex(bytes_data))
        except Exception as e:
            print("str_to_hexstr err(%s)" % e)
            hexstr_data = ""
        return hexstr_data

    def bytes_to_list(self, bytes_data):
        """
        b'\xc4\xe3\xba\xc3AA55' ->
        ['0xc4', '0xe3', '0xba', '0xc3', '0x61', '0x61', '0x35', '0x35']
        """
        try:
            list_data = list(bytes_data)
        except Exception as e:
            print("bytes_to_list err(%s)" % e)
            list_data = []
        return list_data

    def list_to_bytes(self, list_data):
        """
        [0xc4, 0xe3, 0xba, 0xc3, 0x61, 0x61, 0x35, 0x35] ->
        [196, 227, 186, 195, 65, 65, 53, 53]
        b'\xc4\xe3\xba\xc3AA55'
        """
        try:
            bytes_data = bytes(list_data)
        except Exception as e:
            print("list_to_bytes err(%s)" % e)
            bytes_data = b''
        return bytes_data

    def str_to_list(self, str_data):
        """
        "你好AA55" ->
        ['0xc4', '0xe3', '0xba', '0xc3', '0x61', '0x61', '0x35', '0x35']
        [196, 227, 186, 195, 65, 65, 53, 53]
        """
        bytes_data = self.str_to_bytes(str_data)
        list_data = self.bytes_to_list(bytes_data)
        return list_data

    def list_to_str(self, list_data):
        """
        ['0xc4', '0xe3', '0xba', '0xc3', '0x61', '0x61', '0x35', '0x35']
        [196, 227, 186, 195, 65, 65, 53, 53]  ->
        "你好AA55"
        """
        bytes_data = self.list_to_bytes(list_data)
        str_data = self.bytes_to_str(bytes_data)
        return str_data

    def sht16_to_list(self, sht16_data):
        """
        2个字节整数转列表
        65535 -> [255, 255]
        """
        list_data = []
        try:
            list_data = [(sht16_data >> i & 0xff) for i in (8, 0)]
        except Exception as e:
            print("sht16_to_list err(%s)" % e)
        return list_data

    def list_to_sht16(self, list_data):
        """
        列表转2个字节整数  大端模式
        [255, 255] -> 65535
        """
        sht16_data = -1
        try:
            sht16_data = (list_data[0] << 8) + list_data[1]
        except Exception as e:
            print("list_to_int32 err(%s)" % e)
        return sht16_data

    def list_to_sht16_small(self, list_data):
        """
        列表转2个字节整数  小端模式
        [255, 255] -> 65535
        """
        sht16_data = -1
        try:
            sht16_data = (list_data[1] << 8) + list_data[0]
        except Exception as e:
            print("list_to_int32 err(%s)" % e)
        return sht16_data

    def int32t_to_float(self, int32_data):
        """
        #16进制数据转浮点数
        :param int32_data:要转换的数
        :return float_data:转换结果
        """
        float_data = 0.0
        try:
            float_data = struct.unpack('<f', bytes.fromhex(int32_data))[0]
        except Exception as e:
            print("int32 to float err", e)
        return float_data

    def int32_to_list(self, int32_data):
        """
        4个字节整数转列表
        65535 -> [0, 0, 255, 255]
        """
        list_data = []
        try:
            list_data = [(int32_data >> i & 0xff) for i in (24, 16, 8, 0)]
        except Exception as e:
            print("int32_to_list err(%s)" % e)
        return list_data

    def list_to_int32(self, list_data):
        """
        列表转4个字节整数
        [0, 0, 255, 255] -> 65535
        """
        int32_data = -1
        try:
            int32_data = (list_data[0] << 24) + (list_data[1] << 16) + (list_data[2] << 8) + list_data[3]
        except Exception as e:
            print("list_to_int32 err(%s)" % e)
        return int32_data

    def hexstr_to_list(self, hexstr_data):
        """
        16进制字符串转换成列表
        "c4e3bac33536" -> [196, 227, 186, 195, 53, 54]
        """
        bytes_data = binascii.a2b_hex(hexstr_data)
        list_data = self.bytes_to_list(bytes_data)
        return list_data

    def list_to_hexstr(self, list_data):
        """
        列表转换成16进制字符串
        [196, 227, 186, 195, 53, 54] -> "c4e3bac33536"
        """
        str_data = self.list_to_str(list_data)
        hexstr = self.str_to_hexstr(str_data)
        return hexstr

    def int32_to_bytes(self, int32_data):
        """
        4个字节整数转字节
        65535 -> b'\x00\x00\xff\xff'
        """
        list_data = self.int32_to_list(int32_data)
        bytes_data = self.list_to_bytes(list_data)
        return bytes_data

    def bytes_to_int32(self, bytes_data):
        """
        字节转4个字节整数
         b'\x00\x00\xff\xff' -> 65535
        """
        list_data = self.bytes_to_list(bytes_data)
        int32_data = self.list_to_int32(list_data)
        return int32_data


if __name__ == '__main__':
    pass
